package com.springboot.erp.service.impl;

import com.springboot.erp.entity.JshMaterialCurrentStock;
import com.springboot.erp.mapper.JshMaterialCurrentStockMapper;
import com.springboot.erp.service.JshMaterialCurrentStockService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

@Service
@Slf4j
public class JshMaterialCurrentStockImpl implements JshMaterialCurrentStockService {
    @Resource
    private JshMaterialCurrentStockMapper jshMaterialCurrentStockMapper;
    @Override
    public JshMaterialCurrentStock findById(Integer id) {
        return jshMaterialCurrentStockMapper.findCurrentById(id);
    }

    //更新仓库信息
    @Override
    public void update(JshMaterialCurrentStock jshMaterialCurrentStock) {
        jshMaterialCurrentStockMapper.updateCurrent(jshMaterialCurrentStock);
    }

    //报废产品
    @Override
    public void delete(Integer id) {
        jshMaterialCurrentStockMapper.deleteCurrentById(id);
    }

    //初始化仓库
    @Override
    public String insert(JshMaterialCurrentStock jshMaterialCurrentStock) {
        JshMaterialCurrentStock byId = jshMaterialCurrentStockMapper.findCurrent(jshMaterialCurrentStock.getMaterialId(),jshMaterialCurrentStock.getDepotId());
        if (byId == null) {
            jshMaterialCurrentStockMapper.insertCurrent(jshMaterialCurrentStock);
            return "初始化成功";
        }
        return "初始化失败，该产品已存在";
    }

    //入库
    @Override
    public void addMaterialCurrent(JshMaterialCurrentStock jshMaterialCurrentStock, BigDecimal number) {
        JshMaterialCurrentStock byId = jshMaterialCurrentStockMapper.findCurrent(jshMaterialCurrentStock.getMaterialId(),jshMaterialCurrentStock.getDepotId());
        JshMaterialCurrentStock jshMaterialCurrentStock1 = new JshMaterialCurrentStock(
                jshMaterialCurrentStock.getMaterialId(),
                jshMaterialCurrentStock.getDepotId(),
                byId.getCurrentNumber().add(number)
        );
        jshMaterialCurrentStockMapper.addMaterial(jshMaterialCurrentStock1);
    }
    @Override
    //出库
    public void reduceMaterialCurrent(JshMaterialCurrentStock jshMaterialCurrentStock, BigDecimal number) {
        JshMaterialCurrentStock byId = jshMaterialCurrentStockMapper.findCurrent(jshMaterialCurrentStock.getMaterialId(),jshMaterialCurrentStock.getDepotId());
        JshMaterialCurrentStock jshMaterialCurrentStock1 = new JshMaterialCurrentStock(
                jshMaterialCurrentStock.getMaterialId(),
                jshMaterialCurrentStock.getDepotId(),
                byId.getCurrentNumber().subtract(number)
        );
        if (jshMaterialCurrentStock1.getCurrentNumber().compareTo(BigDecimal.ZERO) >= 0) {
            jshMaterialCurrentStockMapper.addMaterial(jshMaterialCurrentStock1);
        }else {
            log.info("失败");
        }
    }

    @Override
    //调拨入库
    public void changeCurrentNumber(JshMaterialCurrentStock jshMaterialCurrentStock,JshMaterialCurrentStock jshMaterialCurrentStock1,BigDecimal number) {
        int i = 0;
        JshMaterialCurrentStock byId = jshMaterialCurrentStockMapper.findCurrent(jshMaterialCurrentStock1.getMaterialId(),jshMaterialCurrentStock1.getDepotId());
        if (byId.getCurrentNumber().compareTo(number) >= 0) {
            reduceMaterialCurrent(jshMaterialCurrentStock1,number);
            i = 1;
        }
        if (i == 1) {
            addMaterialCurrent(jshMaterialCurrentStock,number);
        }
    }
}
